﻿@using System.Text
@using KRM.Core
@model KRM.Core.tblOrder
<table class="table table-hover table-bordered table-fix">
    <thead>
        <tr>
            <th></th>
            <th style="cursor: pointer">OrderID</th>
            <th onclick="sortemailhistory($(this));" data-sort="asc" style="cursor: pointer">From Email</th>
            <th onclick="sortemailhistory($(this));" data-sort="asc" style="cursor: pointer">To Email</th>
            <th onclick="sortemailhistory($(this));" data-sort="asc" style="cursor: pointer">Date Send</th>
            <th style="cursor: pointer">Include Attachment</th>
        </tr>
    </thead>
    <tbody class="bodyemail-history">
        @foreach (var item in Model.LstEmailHistories)
        {
            var kybo = "";
            var action = "";
            var mes = new StringBuilder();
            var attachmentsList = new List<string>();
            string fileName = "";
            try
            {
                string folder = tblCompanyVariable.Folders_CustomerEmails + "\\" + item.DateSent.Year + "\\" + item.DateSent.Month + "\\" + item.DateSent.Day;

                string[] AllFiles = Directory.GetFiles(folder, item.CustomerEmailHistoryID.ToString() + "*");
                if (AllFiles.Any())
                {
                    fileName = AllFiles[0];
                }
                string attachment = item.EmailAttachment != null ? item.EmailAttachment.FileName : "";
                attachmentsList = attachment.Split(';').ToList();
                string[] allLine = System.IO.File.ReadAllLines(fileName);
                if (allLine.Length > 0)
                {
                    kybo = allLine[0].ToString().Replace("KyboInfo: ", "").Replace("<p/>", "");
                    action = allLine[1].ToString().Replace("Action: ", "").Replace("<p/>", "");
                }
                for (int i = 2; i < allLine.Length; i++)
                {
                    mes.AppendLine(allLine[i]);
                }
            }
            catch { }
            <tr>
                <td>
                    @if (item.Fromaddress != "SMS")
                    {
                        <a href="#email@(item.CustomerEmailHistoryID)" role="button" class="btn" data-toggle="modal" style="padding: 5px 3px">...
                        </a>
                        <div id="email@(item.CustomerEmailHistoryID)" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" data-width="800">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                <h4 id="myModalLabel">Order Message</h4>
                            </div>
                            <div class="modal-body">
                                <div class="row-fluid">
                                    <div class="span6">
                                        <div class="row-fluid">
                                            <div class="span4">Kybo Info</div>
                                            <div class="span8">
                                                <input type="text" class="input-block-level" value="@kybo"/>
                                            </div>
                                        </div>
                                        <div class="row-fluid">
                                            <div class="span4">Action</div>
                                            <div class="span8">
                                                <input type="text" class="input-block-level" value="@action"/>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="span6">
                                        <div class="span4 text-right">
                                            Attachment(s): 
                                        </div>
                                        <div class="span8">
                                            @foreach (string t in attachmentsList)
                                            {
                                                Html.Raw("<p>- " + t + "</p>");
                                            }
                                        </div>
                                    </div>
                                </div>
                                <iframe width="100%" height="250px" frameborder="0" src="/OrderView/GetEmailHtml?path=@Server.UrlEncode(fileName)"></iframe>
                                @*<div class="emailhistorycontent" style="height: 250px">
                                
                            </div>*@
                            </div>
                        </div>
                    }
                    else
                    {
                        <a href="#sms@(item.CustomerEmailHistoryID)" role="button" class="btn" data-toggle="modal" style="padding: 5px 3px">...
                        </a>
                        <div style="background: transparent; border: 0; box-shadow: none" id="sms@(item.CustomerEmailHistoryID)" class="modal hide fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" >
                            <div class="modal-body smsdetail">
                                <div >
                                    <div class="row-fluid">
                                        <div class="span1">To: </div>
                                        <div class="span11">@item.Toaddress</div>
                                    </div>
                                    <div class="row-fluid">
                                        <div class="span1">Date: </div>
                                        <div class="span11">@item.DateSent</div>
                                    </div>
                                    <div class="row-fluid" style="padding: 0 10px">
                                      
                                        <div class="span12" style="word-break: break-all;white-space: initial;height: 130px; overflow:auto; padding:0 20px 0 0 ">@Html.Raw(item.Subject) </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    }
                </td>
                <td>@item.OrderID</td>
                <td>@item.Fromaddress</td>
                <td>@item.Toaddress</td>
                <td>@item.DateSent</td>
                <td style="text-align: center">
                    <input type="checkbox" disabled="disabled" @(item.AttachmentID != null ? "checked=checked" : "") />
                </td>
            </tr>
        }
    </tbody>
</table>
